package com.ebt.m.proposal_v2.base;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.ebt.m.R;
import com.ebt.m.proposal_v2.utils.ViewUtils;

import butterknife.ButterKnife;

/**
 * @author archer.qi
 *         Created on 2017/5/10.
 */
public abstract class CompatDialog extends Dialog {
    public Context mContext;
    private View contentView;
    private View dialogView;

    public CompatDialog(Context context) {
        super(context, R.style.sweet_dialog);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView = ViewUtils.inflate(mContext, contentViewResource());
        setContentView(contentView);
        ButterKnife.bind(this, contentView);
        dialogView = getWindow().getDecorView().findViewById(android.R.id.content);
        onCreateView();
    }

    protected abstract int contentViewResource();

    protected abstract void onCreateView();

    public View getDialogView() {
        return dialogView;
    }

    public View find(int id) {
        if (contentView == null) {
            throw new NullPointerException("contentView has not init, please call contentViewResource to return valid layout");
        }
        return contentView.findViewById(id);
    }

    public void toggle() {
        if (isShowing()) {
            dismiss();
        } else {
            show();
        }
    }

}
